package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/7/27 15:12
 *
 * 查询品牌实现类
 *
 */
@Service
public class BrandServiceImpl implements  BrandService {

    @Autowired
    private DtsBrandMapper brandMapper;

    //服务器地址
    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;

    //子账号
    @Value("${aliyun.oss.accessKeyId}")
    private  String accessKeyId;

    //子账号密码
    @Value("${aliyun.oss.accessKeySecret}")
    private  String accessKeySecret;

    //桶的名字
    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param id
     * @param name
     * @return
     */
    @Override
    public IPage<DtsBrand> queryBrandPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {

        //1:创建分页对象.第一参数:当前页,第二参数,每页显示条数
        IPage<DtsBrand> ipage = new Page<>(page, limit);

        //2:创建查询对象
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件是否为空
        if (id != null){
           queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name", name);
        }

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }
         ipage = brandMapper.selectPage(ipage, queryWrapper);

        return ipage;
    }

    /**
     * 删除
     * @param brand
     * @return
     */
    @Override
    public void delete(DtsBrand brand) {
        brandMapper.deleteById(brand);
    }

    /**
     * 添加
     * @param brand
     * @return
     */
    @Override
    public int add(DtsBrand brand) {
        int insert = brandMapper.insert(brand);
        return insert;
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @Override
    public int update(DtsBrand brand) {
        int update = brandMapper.updateById(brand);
        return update;
    }


}
